package com.code4hire.desktop.converter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesUtil {

    public static final String VERSION = "version";
    private static final String PROPERTIES_FILENAME = "config.properties";

    private Properties prop = new Properties();

    public PropertiesUtil() {
        super();
        try {
            if (new File(PROPERTIES_FILENAME).exists()) {
                prop.load(new FileInputStream(PROPERTIES_FILENAME));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void save(String key, String value) {
        try {
            // set the properties value
            prop.setProperty(key, value);

            // save properties to project root folder
            prop.store(new FileOutputStream(PROPERTIES_FILENAME), null);

        } catch (IOException ex) {
            // TODO log
            ex.printStackTrace();
        }
    }

    public String read(String key) {
        return prop.getProperty(key);
    }

}
